<?php

namespace app\index\controller;

use tian\Session;

class Role extends Base
{
    //角色选择大厅
    public function index()
    {
        try {
            $isRole = Session::has('uid');
            if ($isRole != false) {
                $temp = [
                    'uid'=>Session::get('uid'),
                    'sfzx'=>0
                ];
                model('Role')->getInfo($temp);
            }

            $listRole = model('Role')->getList(['sid' => $this->userInfo['id']]);
            $this->assign('listRole', $listRole);

            $this->fetch('index');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    //角色创建
    public function add()
    {
        try {
            $request = request();
            if ($request->method() == 'POST') {
                $role_id = Session::get('uid');
                $modelRole = model('Role');

                $uname = $request->post('uname');
                $usex = $request->post('usex');

                if ($uname == null) {
                    returnJson(0, '请输入昵称');
                }

                $where['uname'] = $uname;
                $infoRole = $modelRole->where($where)->find();
                if (!empty($infoRole)) {
                    returnJson(0, '该昵称已被占用');
                }

                $infoGameConfig = model('GameConfig')->getInfo();

                //创建角色
                $add['sid'] = $this->userInfo['id'];
                $add['uname'] = $uname;
                $add['usex'] = $usex;
                $add['nowmid'] = $infoGameConfig['firstmid'];
                $add['addtime'] = time();

                $add['uczb'] = 100;
                $add['uyxb'] = 2000;
                $add['utz'] = 5;
                $add['ugg'] = 1;
                $add['ull'] = 1;
                $add['umj'] = 1;

                $getId = $modelRole->add($add);
                if ($getId) {
                    //四维属性折算
                    $modelRole->setAttributeConvert($getId);

                    //发放新手礼包
                    $giftBag = [23, 24, 50, 51];
                    foreach ($giftBag as $item) {
                        model('RoleEquip')->addzb($this->userInfo['id'], $item);
                    }

                    //添加通知
                    $temp['name'] = '系统通知';
                    $temp['msg'] = "万中无一的 {$uname} 踏上了仙途";
                    $temp['addtime'] = time();
                    model('Message')->add($temp);
                }

                $res['url'] = url('index');
                returnJson(1, '登录成功', $res);
            } else {
                $this->fetch('add');
            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    //角色选择
    public function optRole()
    {
        try {
            $request = request();
            if ($request->method() == 'POST') {
                $modelRole = model('Role');

                $uid = $request->post('uid');

                $infoRole = $modelRole->getInfo($uid);
                if (empty($infoRole)) {
                    returnJson(0, '角色异常');
                }
                if ($infoRole['isDel']) {
                    returnJson(0, '角色异常');
                }

                //更新登录信息
                $temp = [
                    'uid' => $infoRole['uid'],
                    'endtime' => date('Y-m-d H:i:s'),
                    'sfzx' => 1
                ];
                $modelRole->saved($temp);

                Session::set('uid', $infoRole['uid']);

                $res['url'] = url('home/index');
                returnJson(1, '登录成功', $res);
            } else {
                returnJson(0, '非法请求');
            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    //数据状态
    public function state()
    {
        try {
            $this->role_id = Session::get('uid');

            //获取用户信息
            $info = model('Role')->getRoleAttribute($this->role_id);
            $this->assign('info', $info);
            //echo "<pre>";print_r($info);die;

            $roleStatus = state_hone($info['ulv']);
            $this->assign('roleStatus', $roleStatus);

            $this->fetch('../role/state');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }


}
